package com.dropTables.TakingNote;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LocationEditActivity extends Activity 
{
	public static Button loc_confirm;
	
	public static EditText loc_addr_get;
	
	
	private TextView show_loc;
	public static EditText rSearchText;
	
	
	private String loc_gps;
	private String loc_name;
	public static String loc_str;
	
	public static  String no_loc = "No location selected.";
	
	public static final String KEY_LIST_STRING = "loc_list";
	public static final String KEY_SELECTED_LOC = "selected_loc";
	public static final String KEY_SELECTED_NAME = "selected_name";
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_edit);
		
		Bundle extras = getIntent().getExtras();
		
		loc_confirm = (Button) findViewById(R.id.location_edit_confirm);		//Match items to xml files
		show_loc = (TextView) findViewById(R.id.loc_edit_show_cur_loc);
		
		loc_addr_get = (EditText) findViewById(R.id.loc_addr_enter);
		rSearchText = (EditText) findViewById(R.id.search);
		

		String gps = null, name = null, full = null;
		if(extras != null)														//Get arguments
		{
			full = extras.getString(ReminderDbHelper.KEY_S_LOC);
			if(full != null && !full.equals(no_loc))
			{
				String lst[] = full.split(":");
				if (lst.length > 1)
				{
					name = lst[0];
					gps = lst[1];
					loc_addr_get.setText(name);
				}
			}
			if (NoteEditActivity.gen_loc != null) 
			{
				rSearchText.setText(extras.getString(ReminderDbHelper.KEY_G_LOC));
			}
			
		}
		
		if(gps != null && name != null)						//Handle arguments
		{
			loc_gps = gps;
			loc_name = name;
			loc_str = full;
			show_loc.setText(name);
		}
		else
		{
			loc_gps = "";
			loc_name = no_loc;
			loc_str = no_loc;
		}
		
		show_loc.setText(loc_name);										//Set location display
		
		
		loc_confirm.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				confirm();
			}
		});
		
		
	}
	
	/*
	 * @param voidlastKnownLocation; = locationManager.getLastKnownLocation(locationProvider);
	 * @return void
	 * 
	 */
	
	private void confirm()
	{
		String title, body, search = "", tStamp = "";
		title = NoteEditActivity.title;
		body = NoteEditActivity.body;
		search = rSearchText.getText().toString();
		
		
		//if user doesn't set title and body, put a default
		if (title.equals(""))
		{
			title = "Title";
		}
		if (body.equals(""))
		{
			body = "body";
		}
		//Get the date-time formatted string.
		
		Bundle bundle = new Bundle();
		bundle.putString(ReminderDbHelper.KEY_TITLE, title);
		bundle.putString(ReminderDbHelper.KEY_BODY, body);
		bundle.putString(ReminderDbHelper.KEY_G_LOC, search);
		bundle.putString(ReminderDbHelper.KEY_S_LOC, loc_addr_get.getText().toString());
		
		if(NoteEditActivity.hour>0)														//Set time if time not set
		{	
			int hour=NoteEditActivity.hour;
			int min =NoteEditActivity.min;
			int day=NoteEditActivity.day;
			int month =NoteEditActivity.month;
			int year = NoteEditActivity.year;
			tStamp = String.format("%04d",year)+"-"+String.format("%02d",month+1)+"-"+String.format("%02d",day)+" "+String.format("%02d",hour)+":"+String.format("%02d",min)+":00";

		}
		bundle.putString(ReminderDbHelper.KEY_TIME, tStamp);
		if (NoteEditActivity.rRowId != -1) 
		{
			bundle.putLong(ReminderDbHelper.KEY_ROWID, NoteEditActivity.rRowId);
			bundle.putLong(ReminderDbHelper.KEY_R_ID,NoteEditActivity.rId);
		}
		Intent rIntent = new Intent();
		rIntent.putExtras(bundle);
		getParent().setResult(RESULT_OK, rIntent);
		finish();
	}

}
